package com.appnet.android.truyencuoi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
	
import com.appnet.android.truyencuoi.db.DataBaseHelper;

public class DetailActivity extends Activity {
	private DataBaseHelper mDbHelper;
	private int mCurrentId = ComicConstant.INVALID_ID;
	private int mCurrentCategory = ComicConstant.INVALID_ID;
	private TextView mTvContent;
	private TextView mTvTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		//
		mTvContent = (TextView) findViewById(R.id.tv_detail);
		mTvTitle = (TextView) findViewById(R.id.tv_title);
		//
		Intent intent = getIntent();
		if (intent == null) {
			return;
		}
		mCurrentId = intent.getIntExtra(ComicConstant.EXTRA_ID, -1);
		if (mCurrentId == ComicConstant.INVALID_ID) {
			return;
		}
		mDbHelper = new DataBaseHelper(getApplicationContext());
		Story story = mDbHelper.getContent(mCurrentId);
		if(story != null) {
			mCurrentCategory = story.mCategory;
			mTvContent.setText(story.mContent);
			setTitle(ComicUtils.getCategoryTitle(getApplicationContext(), story.mCategory));
			mTvTitle.setText(story.mTitle);
		}
		//
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_detail, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(mCurrentCategory == ComicConstant.INVALID_ID || mCurrentId == ComicConstant.INVALID_ID) {
			return false;
		}
		Story story = null;
		if(item.getItemId() == R.id.menu_back) {
			story = mDbHelper.getPreContent(mCurrentCategory, mCurrentId);
		}
		else if(item.getItemId() == R.id.menu_next) {
			story = mDbHelper.getNextContent(mCurrentCategory, mCurrentId);
		}
		if(story != null) {
			mTvContent.setText(story.mContent);
			setTitle(ComicUtils.getCategoryTitle(getApplicationContext(), story.mCategory));
			mTvTitle.setText(story.mTitle);
			mCurrentId = story.mId;
		}
		return true;
	}

}
